#pragma once
#include "AccCore.h"    // AIMCC main header
#include "AccSupport.h" // AIMCC C++ helper classes
#include "hatBox.h"
#include "BuddyList.h"
#include "BuddyListDlg.h"
#ifdef _MSC_VER
#pragma comment(lib, "acccore.lib") // aimcc main lib
#pragma comment(lib, "accuuidlib.lib") // aimcc uuid lib
#endif
using namespace AIMCC;

class CHelpClass : public CAccEventSink
{
private:
	CAccPtr<IAccSession> m_sp; //smart pointer
	CWnd* m_cwnd; //handle to the main dialog box, for message box etc
	chatBox* m_IMs;
	CBuddyListDlg* m_buddyList;
public:
	CHelpClass(void);
	CHelpClass(CWnd* h);
	~CHelpClass(void);
	HRESULT Init(CString userName, CString password)
	{
		HRESULT hr;
        if (SUCCEEDED(hr = AccCreateSession(IID_IAccSession, (void**)&m_sp)) &&
			SUCCEEDED(hr = Advise(m_sp)))  //if session is loaded into smart pointer
        {
            CAccPtr<IAccClientInfo> spClientInfo;
            hr = m_sp->get_ClientInfo(&spClientInfo);
            if (SUCCEEDED(hr))
            {
				//convert name and password to unicode
				wchar_t* usN = T2W(userName.GetBuffer());
				wchar_t* pw = T2W(password.GetBuffer());

				CAccVariant desc(L"accsample (key=ob1FeBQ2J0g1LnfZ)");	//custom client key
                spClientInfo->put_Property(AccClientInfoProp_Description, desc); //identify client on aim network
                if (SUCCEEDED(hr = m_sp->put_Identity(usN)))
                    hr = m_sp->SignOn(pw);	//login info
            }
        } 
        return hr;
	}

	void OnStateChange(IAccSession* piSession, AccSessionState state, AccResult hr)
	{
		if(hr == ACC_E_INVALID_PASSWORD) m_cwnd->MessageBox(_T("PASSWORD INVALID"),_T("Sign In Error"));
		if (state == AccSessionState_Offline)
            AccPostQuit(hr);
		
		//if signing on, load buddy list
		if(state == AccSessionState_Online)
		{
			m_buddyList = new CBuddyListDlg(m_cwnd,piSession);
			m_buddyList->Create(131,m_buddyList->GetDesktopWindow());
			m_buddyList->ShowWindow(SW_SHOW);
			//m_buddyList->loadBuddyList();
		}
	}
	HRESULT Run()
	{
		return AccMessageLoop();
	}
	void Term()
	{
		Unadvise(m_sp);
        m_sp = NULL;
	}
	
	void OnSecondarySessionStateChange(IAccSession* piSession, 
		IAccSecondarySession* piSecSession, AccSecondarySessionState state, AccResult hr)
    {
        // always accept incoming IM sessions                
        if (state == AccSecondarySessionState_ReceivedProposal)
        {
            AccSecondarySessionServiceId id;
            piSecSession->get_ServiceId(&id);
            if (id == AccSecondarySessionServiceId_Im)
                piSecSession->Accept();  
        }                    
    }    
    
	void OnImReceived(
        IAccSession* piSession, IAccImSession* piImSession, IAccParticipant* piSender, IAccIm* piIm)
    {
		//put the text into the display box of an IMBox control
		BSTR tx, user; //BSTR wrapper that includes append
        piIm->GetConvertedText(OLESTR("text/plain"), &tx);
		piSender->get_Name(&user);
		if(m_IMs == (CWnd*)NULL)
		{
			m_IMs = new chatBox();
			m_IMs->Create(130,m_cwnd->GetDesktopWindow()); //use the desktop as the parent
									  					  //this allows it to float nice
			m_IMs->SetWindowTextW((CString)user);
		}
			m_IMs->ShowWindow(SW_SHOW);
			m_IMs->receiveMsg(&user, &tx);
    }  

	void signOff()
	{
		m_sp->SignOff();
	}
};
